/*************************************************************************
> File Name: 02_test.cpp
> Author: hzy
> Created Time: 2023-08-15 20:49:15
> Description: 构造一个数组类，实现任意数据元素类型的数据元素的存储
************************************************************************/
#include <iostream>

using namespace std;

template <class T>
class Array
{
private:
    T *data;
    int length;

public:
    Array(int mylength)
    {
        length = mylength;
        data = new T[length];
    }
    ~Array()
    {
        if (data != NULL)
        {
            delete[] data;
        }
    }

    void printfArr();

    T &operator[](int i);
};

template <class T>
void Array<T>::printfArr()
{
    for (int i = 0; i < length; i++)
    {
        cout << data[i] << "  ";
    }
    cout << endl;
}

template <class T>
T &Array<T>::operator[](int i)
{
    return data[i];
}

void test_01()
{
    Array<int> arr1(10);

    for (int i = 0; i <= 5; i++)
    {
        arr1[i] = i + 1;
    }

    arr1.printfArr();
}

void test_02()
{
    Array<char> arr(10);

    for (int i = 0; i <= 9; i++)
    {
        arr[i] = 'a' + i;
    }

    arr.printfArr();
}

int main()
{
    test_01();
    cout << "==========" << endl;
    test_02();

    return 0;
}